import java.util.List;
import java.util.Scanner;

public class Menu {
    private Scanner scanner;
    private UserService userService;
    private MovieService movieService;
    private ScheduleService scheduleService;
    private TicketService ticketService;
    private User currentUser;

    public Menu() {
        this.scanner = new Scanner(System.in);
        this.userService = new UserService(scanner);
        this.movieService = new MovieService(scanner);
        this.scheduleService = new ScheduleService(scanner);
        this.ticketService = new TicketService(scanner);
        this.currentUser = null;
    }

    // 显示登录界面
    public void showLoginScreen() {
        while (currentUser == null) {
            System.out.println("\n===== 影院管理系统 V1.1 =====");
            System.out.println("1. 登录");
            System.out.println("2. 退出系统");
            System.out.print("请选择操作: ");

            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    currentUser = userService.login();
                    if (currentUser != null) {
                        showMainMenu();
                    }
                    break;
                case "2":
                    System.out.println("感谢使用，再见！");
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效的选择，请重试！");
            }
        }
    }

    // 显示主菜单（根据角色不同显示不同菜单）
    private void showMainMenu() {
        while (currentUser != null) {
            System.out.println("\n===== 欢迎使用影院管理系统 =====");
            System.out.println("当前用户: " + currentUser.getUsername() + " (" + currentUser.getRole() + ")");

            if ("admin".equals(currentUser.getRole())) {
                showAdminMenu();
            } else if ("manager".equals(currentUser.getRole())) {
                showManagerMenu();
            } else if ("front".equals(currentUser.getRole())) {
                showFrontMenu();
            } else {
                System.out.println("未知角色，强制退出登录！");
                currentUser = null;
            }
        }
    }

    // 管理员菜单
    private void showAdminMenu() {
        System.out.println("\n----- 管理员操作菜单 -----");
        System.out.println("1. 用户管理");
        System.out.println("2. 修改个人密码");
        System.out.println("3. 退出登录");
        System.out.print("请选择操作: ");

        String choice = scanner.nextLine();
        switch (choice) {
            case "1":
                showUserManagementMenu();
                break;
            case "2":
                handleChangePassword();
                break;
            case "3":
                System.out.println("退出登录成功！");
                currentUser = null;
                break;
            default:
                System.out.println("无效的选择，请重试！");
        }
    }

    // 用户管理子菜单
    private void showUserManagementMenu() {
        System.out.println("\n----- 用户管理 -----");
        System.out.println("1. 列出所有用户");
        System.out.println("2. 添加用户");
        System.out.println("3. 删除用户");
        System.out.println("4. 重置用户密码");
        System.out.println("5. 返回上一级");
        System.out.print("请选择操作: ");

        String choice = scanner.nextLine();
        switch (choice) {
            case "1":
                userService.listAllUsers();
                break;
            case "2":
                userService.addUser();
                break;
            case "3":
                System.out.print("请输入要删除的用户名: ");
                String delUser = scanner.nextLine();
                userService.deleteUser(delUser);
                break;
            case "4":
                System.out.print("请输入要重置密码的用户名: ");
                String resetUser = scanner.nextLine();
                System.out.print("请输入新密码: ");
                String newPwd = scanner.nextLine();
                userService.resetPassword(resetUser, newPwd);
                break;
            case "5":
                return;
            default:
                System.out.println("无效的选择，请重试！");
        }
    }

    // 经理菜单
    private void showManagerMenu() {
        System.out.println("\n----- 经理操作菜单 -----");
        System.out.println("1. 影片管理");
        System.out.println("2. 排片管理");
        System.out.println("3. 查看销售数据");
        System.out.println("4. 修改个人密码");
        System.out.println("5. 重置前台用户密码");
        System.out.println("6. 退出登录");
        System.out.print("请选择操作: ");

        String choice = scanner.nextLine();
        switch (choice) {
            case "1":
                showMovieManagementMenu();
                break;
            case "2":
                showScheduleManagementMenu();
                break;
            case "3":
                showSalesData();
                break;
            case "4":
                handleChangePassword();
                break;
            case "5":
                System.out.print("请输入要重置密码的前台用户名: ");
                String resetUser = scanner.nextLine();
                System.out.print("请输入新密码: ");
                String newPwd = scanner.nextLine();
                userService.resetPassword(resetUser, newPwd);
                break;
            case "6":
                System.out.println("退出登录成功！");
                currentUser = null;
                break;
            default:
                System.out.println("无效的选择，请重试！");
        }
    }

    // 影片管理子菜单
    private void showMovieManagementMenu() {
        System.out.println("\n----- 影片管理 -----");
        System.out.println("1. 列出所有影片");
        System.out.println("2. 添加影片");
        System.out.println("3. 修改影片");
        System.out.println("4. 删除影片");
        System.out.println("5. 查找影片");
        System.out.println("6. 返回上一级");
        System.out.print("请选择操作: ");

        String choice = scanner.nextLine();
        switch (choice) {
            case "1":
                movieService.listAllMovies();
                break;
            case "2":
                movieService.addMovie();
                break;
            case "3":
                movieService.updateMovie();
                break;
            case "4":
                movieService.deleteMovie();
                break;
            case "5":
                movieService.findMovie();
                break;
            case "6":
                return;
            default:
                System.out.println("无效的选择，请重试！");
        }
    }

    // 排片管理子菜单
    private void showScheduleManagementMenu() {
        System.out.println("\n----- 排片管理 -----");
        System.out.println("1. 列出所有场次");
        System.out.println("2. 添加场次");
        System.out.println("3. 修改场次");
        System.out.println("4. 删除场次");
        System.out.println("5. 按影片查询场次");
        System.out.println("6. 返回上一级");
        System.out.print("请选择操作: ");

        String choice = scanner.nextLine();
        switch (choice) {
            case "1":
                scheduleService.listAllSchedules();
                break;
            case "2":
                scheduleService.addSchedule();
                break;
            case "3":
                scheduleService.updateSchedule();
                break;
            case "4":
                scheduleService.deleteSchedule();
                break;
            case "5":
                System.out.print("请输入影片ID: ");
                String movieId = scanner.nextLine();
                scheduleService.listSchedulesByMovieId(movieId);
                break;
            case "6":
                return;
            default:
                System.out.println("无效的选择，请重试！");
        }
    }

    // 前台菜单
    private void showFrontMenu() {
        System.out.println("\n----- 前台操作菜单 -----");
        System.out.println("1. 查看影片列表");
        System.out.println("2. 查看场次列表");
        System.out.println("3. 售票");
        System.out.println("4. 退票");
        System.out.println("5. 修改个人密码");
        System.out.println("6. 退出登录");
        System.out.print("请选择操作: ");

        String choice = scanner.nextLine();
        switch (choice) {
            case "1":
                movieService.listAllMovies();
                break;
            case "2":
                scheduleService.listAllSchedules();
                break;
            case "3":
                ticketService.sellTicket();
                break;
            case "4":
                ticketService.refundTicket();
                break;
            case "5":
                handleChangePassword();
                break;
            case "6":
                System.out.println("退出登录成功！");
                currentUser = null;
                break;
            default:
                System.out.println("无效的选择，请重试！");
        }
    }

    // 处理修改密码
    private void handleChangePassword() {
        System.out.print("请输入原密码: ");
        String oldPwd = scanner.nextLine();
        System.out.print("请输入新密码: ");
        String newPwd = scanner.nextLine();
        System.out.print("请再次输入新密码: ");
        String confirmPwd = scanner.nextLine();

        if (!newPwd.equals(confirmPwd)) {
            System.out.println("两次输入的新密码不一致！");
            return;
        }

        userService.changePassword(currentUser, oldPwd, newPwd);
    }

    // 显示销售数据（简化版）
    private void showSalesData() {
        System.out.println("\n----- 销售数据统计 -----");
        System.out.println("1. 当日销售数据");
        System.out.println("2. 当周销售数据");
        System.out.println("3. 当月销售数据");
        System.out.println("4. 返回上一级");
        System.out.print("请选择操作: ");

        String choice = scanner.nextLine();
        switch (choice) {
            case "1":
                calculateSalesData("当日");
                break;
            case "2":
                calculateSalesData("当周");
                break;
            case "3":
                calculateSalesData("当月");
                break;
            case "4":
                return;
            default:
                System.out.println("无效的选择，请重试！");
        }
    }

    // 计算销售数据（简化版）
    private void calculateSalesData(String period) {
        // 这里简化处理，实际应该根据日期筛选
        List<Schedule> schedules = DataAccess.getAllSchedules();
        int totalTickets = 0;
        double totalRevenue = 0.0;

        for (Schedule schedule : schedules) {
            Movie movie = DataAccess.findMovieById(schedule.getMovieId());
            if (movie == null) continue;

            // 计算已售座位数
            int soldSeats = 0;
            for (boolean seat : schedule.getSeats()) {
                if (seat) soldSeats++;
            }

            totalTickets += soldSeats;
            totalRevenue += soldSeats * movie.getPrice();
        }

        System.out.println("\n===== " + period + "销售数据 =====");
        System.out.println("总售票数: " + totalTickets + " 张");
        System.out.println("总销售额: " + totalRevenue + " 元");
    }
}
